package com.guoqi.oabridge.bridgeImpl

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import com.alibaba.fastjson.JSONObject
import com.guoqi.oabridge.Callback
import com.tencent.smtt.sdk.WebView

interface IBridge
/**
 * 设备
 * @author GUOQI
 */
open class DeviceImpl : IBridge {

    companion object {

        /**
         * 去拨号界面
         */
        @JvmStatic
        open fun makePhoneCall(webView: WebView, param: JSONObject, callback: Callback) {
            val phoneNumber = param.getString("phoneNumber")
            if (!phoneNumber.isNullOrEmpty()) {
                val call = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$phoneNumber"))
                webView.context.startActivity(call)
            }
        }

        /**
         * 去置界面
         */
        @JvmStatic
        open fun toApplicationSettings(webView: WebView, param: JSONObject, callback: Callback) {
            val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
            intent.data = Uri.fromParts("package", webView.context.packageName, null)
            webView.context.startActivity(intent)
        }


        /**
         * 剪切板
         */
        @JvmStatic
        open fun setClipboardData(webView: WebView, param: JSONObject, callback: Callback) {
            val clipData = param.getString("data")
            if (!clipData.isNullOrEmpty()) {
                val cm = webView.context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                cm.primaryClip = ClipData.newPlainText(null, clipData)
            }
        }
        /**
         * 粘贴
         */
        @JvmStatic
        open fun getClipboardData(webView: WebView, param: JSONObject, callback: Callback): String {
            val cm = webView.context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            if (cm.primaryClip != null && cm.primaryClip.itemCount > 0) {
                return cm.primaryClip.getItemAt(0).text.toString()
            }
            return ""
        }
    }
}
